﻿using DAL;
using DAL.DB;
using DAL.Model;
using DcpServer.Common.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfService
{
    /// <summary>
    /// 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码和配置文件中的类名“Service1”。
    /// </summary>
    public class Service : IService
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        /// <summary>
        /// GetDataUsingDataContract
        /// </summary>
        /// <param name="composite">composite</param>
        /// <returns>DataUsingDataContract</returns>
        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }

            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }

            return composite;
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns>string</returns>
        public string GetRTData()
        {
            string hr = "HR:120,";
            string[] spo2 = data();
            string strIds = string.Join(",", spo2);
            string nibp = "NIBP:65/135";
            string[] ecg1 = ECG();
            string strIdss = string.Join(",", ecg1);
            string jsonText = "{" + hr + "spo2:[{" + strIds + "}]," + nibp + "," + "ECG1:[{" + strIdss + "}]" + "}";

            LogHelper.WriteLog(jsonText);
            string time = "TimeDate:" + DateTime.Now + "------------------------\r\n" + jsonText;

            return jsonText;
        }

        public string[] data()
        {
            Random b = new Random();
            string[] spo2 = new string[99];
            for (int i = 0; i < spo2.Length; i++)
            {
                spo2[i] = b.Next(1, 101).ToString();
            }

            return spo2;
        }

        public string[] ECG()
        {
            Random b = new Random();
            string[] spo2 = new string[500];
            for (int i = 0; i < spo2.Length; i++)
            {
                spo2[i] = b.Next(1, 101).ToString();
            }

            return spo2;
        }

        public int ThisDayPatCount()
        {
            using (var db = new YFrameContext())
            {
                DateTime startTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
                DateTime endTime = new DateTime(DateTime.Now.AddDays(1).Year, DateTime.Now.AddDays(1).Month, DateTime.Now.AddDays(1).Day);
                var dateCount = db.PatientInfo.Where(u => u.CreateTime >= startTime && u.CreateTime < endTime).ToList();
                return dateCount.Count() + 1;
            }
        }

        /// <summary>
        /// 根据采样率   查询最新一秒的数据
        /// </summary>
        /// <param name="samp">samp</param>
        /// <param name="patid">patid</param>
        /// <returns>最新一秒的数据</returns>
        public string NowTime_SampData(int samp, string patid)
        {
            using (var db = new YFrameContext())
            {
                DateTime endTime = DateTime.Now;
                DateTime startTime = DateTime.Now.AddSeconds(-2);
                Random ran = new Random();
                var modelOld = db.WaveData.OrderBy(u => u.CreateTime).Take(1).ToList();

                // var ModelOld = db.XD_MapData.Where(u => u.PatientID == PatID && u.IsState == 1 && u.CreateTime >= StartTime && u.CreateTime < endTime).OrderByDescending(u => u.CreateTime).Take(samp).ToList();
                double nibpMax = 0;
                double nibpMin = 0;
                string txtNibp = string.Empty;
                string txtEcg = string.Empty;
                string txtSpo2 = string.Empty;
                string txtResp = string.Empty;
                string txtTemp = string.Empty;

                if (modelOld.Count != 0)
                {
                    Random rd = new Random();
                    nibpMax = rd.Next(100, 120);
                    nibpMin = rd.Next(70, 80);
                    txtNibp = nibpMax + "/" + nibpMin + "   " + (int)((nibpMax + nibpMin * 2) / 3);
                    txtEcg = rd.Next(75, 80).ToString();
                    txtTemp = 36 + "." + rd.Next(4, 8);
                    txtSpo2 = ran.Next(97, 99).ToString();
                    txtResp = ran.Next(17, 20).ToString();
                }
                else
                {
                    txtTemp = "无数据";
                    txtSpo2 = "无数据";
                    txtResp = "无数据";
                    txtNibp = "无数据";
                    txtEcg = "无数据";
                }

                var json = new
                {
                    TxtTemp = txtTemp,
                    TxtSpo2 = txtSpo2,
                    TxtResp = txtResp,
                    TxtNibp = txtNibp,
                    TxtEcg = txtEcg,
                };
                return JsonConvert.SerializeObject(json);
            }
        }

        /// <summary>
        /// MapData
        /// </summary>
        /// <param name="outTime">outTime</param>
        /// <param name="samp">samp</param>
        /// <param name="patID">patID</param>
        /// <returns>波行数据</returns>
        public string MapData(int outTime, int samp, string patID)
        {
            using (var db = new YFrameContext())
            {
                DateTime endTime = DateTime.Now;
                DateTime startTime = DateTime.Now.AddSeconds(-1);
                var modelOld = db.WaveData.OrderByDescending(u => u.CreateTime).Take(2).ToList();
                return JsonConvert.SerializeObject(modelOld);
            }
        }

        /// <summary>
        /// DataPatShow
        /// </summary>
        /// <returns>病人信息显示</returns>
        public string DataPatShow()
        {
            using (var db = new YFrameContext())
            {
                var patInfo = db.PatientInfo.OrderByDescending(u => u.CreateTime).ToList();
                return JsonConvert.SerializeObject(patInfo);
            }
        }

        public string MapAdd(string str)
        {
            WaveData mapInfo = JsonConvert.DeserializeObject<WaveData>(str);
            //PgsqlHelper.ExecuteSQL("INSERT INTO \"XD_MapData\"(\"MapId\",\"ECG\",\"Temp\",\"Spo2\",\"Resp\",\"NIBPMax\",\"NIBPMin\",\"IsState\",\"IsArchive\",\"PatientID\",\"CreateTime\",\"UpdaTime\") VALUES('" + Guid.NewGuid() + "', " + mapInfo.ECG + ", " + mapInfo.Temp + ", " + mapInfo.Spo2 + "," + mapInfo.Resp + ", " + mapInfo.NIBPMax + "," + mapInfo.NIBPMin + ", " + mapInfo.IsState + "," + mapInfo.IsArchive + " ," + mapInfo.PatientID + ",to_timestamp('" + ((DateTime)mapInfo.CreateTime).ToString("yyyy-MM-dd HH:mm:ss:fff") + "','YYYY-MM-DD HH24:MI:SS.MS'),to_timestamp('" + ((DateTime)mapInfo.UpdaTime).ToString("yyyy-MM-dd HH:mm:ss:fff") + "','YYYY-MM-DD HH24:MI:SS.MS'))");
            
            return string.Empty;
        }

        /// <summary>
        /// 事件回顾信息列表
        /// </summary>
        /// <returns>事件回顾信息</returns>
        public string DataPatShow1()
        {
            using (var db = new YFrameContext())
            {
                var aleInfo = (from i in db.AlarmSetting
                               orderby i.CreateTime descending
                               select i).ToList();
                return JsonConvert.SerializeObject(aleInfo);
            }
        }

        /// <summary>
        /// 报警
        /// </summary>
        /// <returns>Alarm</returns>
        public string Alarm()
        {
            using (var db = new YFrameContext())
            {
                var aleInfo = (from i in db.AlarmSetting
                               orderby i.CreateTime descending
                               select i).ToList();

                return JsonConvert.SerializeObject(aleInfo);
            }
        }
    }
}
